import axios from 'axios'
import storage from './storage'

// 创建axios实例
const instance = axios.create({ timeout: 1000 * 12 })

instance.interceptors.request.use(
  (config) => {
    if (storage.getItem('token')) {
      config['token'] = storage.getItem('token')
    }
    return config
  },
  (err) => Promise.reject(err)
)

instance.interceptors.response.use(
  (res) => {
    if (res.data.code === 200) {
      // 如果还有其他的code码，可以加在elseif里面
      return res.data
    } else if (res.data.code === 0) {
      return res.data
    } else {
      return Promise.reject(res)
    }
  },
  (err) => Promise.reject(err)
)

export default instance
